package com.dynamic.data.permission.config;

import com.dynamic.data.permission.common.util.SpringContextUtil;
import com.dynamic.data.permission.interceptor.RequestInterceptor;
import com.dynamic.data.permission.interceptor.ResourceCheckAspect;
import com.dynamic.data.permission.interceptor.ResourceCodeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//@Order
@Configuration
@EnableConfigurationProperties({PermissionProperties.class, ServerProperties.class})
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    @ConditionalOnMissingBean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor();
    }

    @Bean
    @ConditionalOnMissingBean
    public SpringContextUtil springContextUtil() {
        return new SpringContextUtil();
    }

    @Bean
    @ConditionalOnMissingBean
    public ResourceCodeInterceptor resourceCodeInterceptor() {
        return new ResourceCodeInterceptor();
    }

    @Bean
    @ConditionalOnMissingBean
    public ResourceCheckAspect resourceCheckAspect() {
        return new ResourceCheckAspect();
    }

    @Autowired
    private PermissionProperties permissionProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加请求拦截器
        if (permissionProperties.isEnabled()) {
            registry.addInterceptor(requestInterceptor()).excludePathPatterns(permissionProperties.getExcludePathPatterns());
        }
    }
}
